*************************************
*** Panel Analysis: LISS *************
*************************************

global OUT "SET_YOUR_OUTPATH"

// this is for data which is later read into R
global OUT2 "SET_YOUR_OUTPATH_FOR_DATAFRAMES"	

***create dummy datasets for regsave

*hybrid models
clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\LISS\liss_coef_hhinc.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\LISS\liss_coef_hhinc_dec_pp_sqrt.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\LISS\liss_coef_hhinc_sat.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\LISS\liss_coef_loginc.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\LISS\liss_coef_incdiff2.dta", replace


*bivariate
clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\LISS\liss_coef_hhinc_0.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\LISS\liss_coef_hhinc_dec_pp_sqrt_0.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\LISS\liss_coef_hhinc_sat_0.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\LISS\liss_coef_loginc_0.dta", replace


*equivalence testing
clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\LISS\liss_coef_hhinc_dec_pp_sqrt_e.dta", replace


*lag models
clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\LISS\liss_coef_lag_hhinc.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\LISS\liss_coef_lag_hhinc_dec_pp_sqrt.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\LISS\liss_coef_lag_hhinc_sat.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\LISS\liss_coef_lag_loginc.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\LISS\liss_coef_lag_incdiff2.dta", replace

clear
input coef stderr ci_lower ci_upper N _id
. . . . . .
end
save "$OUT2\LISS\liss_coef_lag_inc_subj_obj.dta", replace





*load data
use "SET_YOUR_INPATH\LISS\liss_long.dta", clear

*relevant variables:
*sep: hhinc (net hh inc, month), satfin (sat. w. fin. sit)
*pol. vars: 
	*interest: polint
	*vote: vote_next (only 2008-2010) vote_retro
	*confidence in institutions: conf_
	*satisfaction with institutions: sat_
	*other: effex effint part party_sup satgov_now vote_conf? 
*socdem: hhsize (num. pers. in hh), age, sex, empstat, edu
*ids: pid year 


xtset pid year


*remove income outliers
replace hhinc=. if hhinc>20000 & hhinc<100000

*use hhinc with standardized hhinc
egen zhhinc = std(hhinc)
replace hhinc = zhhinc

// use income deciles from 1-10
rename hhinc_dec10 hhinc_dec10_
clonevar hhinc_dec10 = hhinc_dec 
 
rename hhinc_dec_pp10 hhinc_dec_pp10_
clonevar hhinc_dec_pp10 = hhinc_dec_pp 
 
rename hhinc_dec_pp_sqrt10 hhinc_dec_pp_sqrt10_
clonevar hhinc_dec_pp_sqrt10 = hhinc_dec_pp_sqrt 


// generate loginc
gen loginc = ln(hhinc_pp_sqrt)


* generate income change/jobloss variables
gen incdiff = hhinc_dec-L.hhinc_dec 

recode incdiff (-9/-1=1 "shock")(0/9=0 "no shock")(else=.), gen(incdiff1)
recode incdiff (-9/-2=1 "shock")(-1 0/9=0 "no shock")(else=.), gen(incdiff2)

// jobloss has only 943 cases on 1
gen jobloss = 0
replace jobloss = . if empstat==.
replace jobloss = 1 if (empstat==4 | empstat==5 | empstat==6 | empstat==11) & (L.empstat<=3 & empstat>0)


*generate additional age interactions
gen hhincXage = hhinc * age18
gen hhinc_ppXage = hhinc_pp * age18
gen hhinc_pp_sqrtXage = hhinc_pp_sqrt * age18

gen incdiff2Xage = incdiff2 * age18


*create engagement scales via factor analysis
reg polint01 vote_next effint10 part10
gen mysample1 = e(sample)
polychoric polint01 vote_next effint10  part10 if mysample1==1

global N = r(sum_w)
matrix r = r(R)
factormat r, n($N) factors(1)
predict f1
sum f1
gen engagement = ((f1 - r(min)) / (r(max)-r(min)))*10


* labels
lab var polint01 "Pol. Interest"
lab var vote_next "Vote"
lab var voteintent10 "Intention to vote"
lab var effint10 "Int. Efficacy"
lab var part10 "Pol. part. index"
lab var engagement "Political Engagement Scale"



**************************************
***** Analysis ***********************
**************************************

estpost summarize polint01 vote_next voteintent10 party_sup10 effint10 effex10 part10 satgov_now10 sat_gov10 conf_gov10 ///
					hhinc hhinc_pp hhinc_pp_sqrt hhinc_dec10 hhinc_dec_pp10 hhinc_dec_pp_sqrt10 satfin female age18 unemployed nlf edu_med edu_high migrant incdiff2 ///
					hhinc_decXage hhinc_dec_ppXage hhinc_dec_pp_sqrtXage  hhinc_satXage incdiff2Xage 
esttab using "$OUT\Tables\LISS\summary_stats_liss.rtf", b(2) cells("count mean(fmt(3)) sd(fmt(3))  min max") replace


* regressions with age (continuous)

// for coef export
foreach var of varlist polint01 vote_next voteintent10 effint10  part10 {
	xthybrid `var' hhinc, clusterid(pid) full
	eststo m001_`var'
	regsave W__hhinc B__hhinc using "$OUT2\LISS\liss_coef_hhinc_0.dta", ci level(95) autoid append
	xthybrid `var' hhinc female age18 unemployed nlf edu_med edu_high migrant, clusterid(pid) full
	eststo m01_`var'
	regsave W__hhinc B__hhinc using "$OUT2\LISS\liss_coef_hhinc.dta", ci level(95) autoid append
	
	xthybrid `var' loginc, clusterid(pid) full
	eststo m002_`var'
	regsave W__loginc B__loginc using "$OUT2\LISS\liss_coef_loginc_0.dta", ci level(95) autoid append
	xthybrid `var' loginc female age18 unemployed nlf edu_med edu_high migrant, clusterid(pid) full
	eststo m02_`var'
	regsave W__loginc B__loginc using "$OUT2\LISS\liss_coef_loginc.dta", ci level(95) autoid append
	
	xthybrid `var' hhinc_dec_pp_sqrt10, clusterid(pid) full
	eststo m03_`var'
	regsave W__hhinc_dec_pp_sqrt10 B__hhinc_dec_pp_sqrt10 using "$OUT2\LISS\liss_coef_hhinc_dec_pp_sqrt_0.dta", ci level(95) autoid append
	xthybrid `var' hhinc_dec_pp_sqrt10 female age18 unemployed nlf edu_med edu_high migrant, clusterid(pid) full
	eststo m3_`var'
	regsave W__hhinc_dec_pp_sqrt10 B__hhinc_dec_pp_sqrt10 using "$OUT2\LISS\liss_coef_hhinc_dec_pp_sqrt.dta", ci level(95) autoid append
	
	xthybrid `var' hhinc_sat10, clusterid(pid) full
	eststo m04_`var'
	regsave W__hhinc_sat10 B__hhinc_sat10 using "$OUT2\LISS\liss_coef_hhinc_sat_0.dta", ci level(95) autoid append
	xthybrid `var' hhinc_sat10 female age18 unemployed nlf edu_med edu_high migrant , clusterid(pid) full
	eststo m4_`var'
	regsave W__hhinc_sat10 B__hhinc_sat10 using "$OUT2\LISS\liss_coef_hhinc_sat.dta", ci level(95) autoid append
}

// for equivalence testing
foreach var of varlist polint01 vote_next voteintent10 effint10  part10  {
	xtreg `var' hhinc_dec_pp_sqrt10 age18 unemployed nlf edu_med edu_high, fe
	regsave hhinc_dec_pp_sqrt10 using "$OUT2\LISS\liss_coef_hhinc_dec_pp_sqrt_e.dta", ci level(90) autoid append
}


// interactions
foreach var of varlist polint01 vote_next voteintent10 effint10 part10  {
	xthybrid `var' hhinc_dec_pp_sqrt10 female age18 unemployed nlf edu_med edu_high migrant hhinc_dec_pp_sqrtXage, clusterid(pid) full
	eststo m7_`var'
}


// two tables
esttab m3_polint01 m7_polint01 m3_vote_next m7_vote_next m3_voteintent10 m7_voteintent10 ///
	m3_effint10 m7_effint10 m3_part10 m7_part10 ///
	using "$OUT\Tables\LISS\hybrid_all_incdec_pp_sqrt.rtf", b(3) se replace	
esttab m3_polint01 m7_polint01 m3_vote_next m7_vote_next m3_voteintent10 m7_voteintent10 ///
	m3_effint10 m7_effint10 m3_part10 m7_part10 ///
	using "$OUT\Tables\LISS\hybrid_all_incdec_pp_sqrt.tex", b(3) se replace label nonumbers booktabs alignment(D{.}{.}{3}) scalars("N N")	///
	varlabels ///
				(R__sex "Sex" ///
				R__migrant "Migrant" ///
				R__edu_med "Edu: medium" ///
				R__edu_high "Edu: high" ///
				W__edu_med "Edu: medium" ///
				W__edu_high "Edu: high" ///
				B__edu_med "Edu: medium" ///
				B__edu_high "Edu: high" ///
				W__age18 "W: Age" ///
				B__age18 "B: Age" ///
				W__unemployed "W: Unemployed" ///
				B__unemployed "B: Unemployed" ///
				W__nlf "W: Not in Labor Force" ///
				B__nlf "B: Not in Labor Force" ///
				W__hhinc "W: Income" ///
				B__hhinc "B: Income" ///
				W__hhincXage "W: Inc. * Age" ///
				B__hhincXage "B: Inc. * Age" ///
				W__hhinc_pp_sqrt10 "W: Income (dec.)" ///
				B__hhinc_pp_sqrt10 "B: Income (dec.)" ///
				W__hhinc_pp_sqrtXage "W: Inc. (dec.) * Age" ///
				B__hhinc_pp_sqrtXage "B: Inc. (dec.) * Age" ///
				W__hhinc_sat "W: Sat. Income" ///
				B__hhinc_sat "B: Sat. Income" ///
				W__hhinc_satXage "W: Sat. Inc. * Age" ///
				B__hhinc_satXage "B: Sat. Inc. * Age" ///
				_cons "Constant")	
				
	

// engagement scale:
xthybrid engagement hhinc_dec_pp_sqrt10 female age18 unemployed nlf edu_med edu_high migrant, clusterid(pid) full
eststo m_engage	
	
esttab m_engage ///
	using "$OUT\Tables\LISS\hybrid_engage_eqinc.tex", b(3) se replace label nonumbers booktabs alignment(D{.}{.}{3}) scalars("N N")	///
	varlabels ///
				(R__female "Female" ///
				R__migrant "Migrant" ///
				R__edu_med "Edu: medium" ///
				R__edu_high "Edu: high" ///
				W__edu_med "Edu: medium" ///
				W__edu_high "Edu: high" ///
				B__edu_med "Edu: medium" ///
				B__edu_high "Edu: high" ///
				W__age18 "W: Age" ///
				B__age18 "B: Age" ///
				W__unemployed "W: Unemployed" ///
				B__unemployed "B: Unemployed" ///
				W__nlf "W: Not in Labor Force" ///
				B__nlf "B: Not in Labor Force" ///
				W__hhinc_pp_sqrt10 "W: Income (dec.)" ///
				B__hhinc_pp_sqrt10 "B: Income (dec.)" ///
				_cons "Constant")
		
		

// test: objective income on subjective income
foreach var of varlist hhinc hhinc_dec_pp_sqrt10 incdiff2 {
	xthybrid hhinc_sat10 `var' female age18 unemployed nlf edu_med edu_high migrant , clusterid(pid) full
	eststo inc_so_`var'
}
esttab inc_so_hhinc inc_so_hhinc_dec_pp_sqrt10 inc_so_incdiff2 ///
	using "$OUT\Tables\LISS\hybrid_all_inc_subj_obj.rtf", b(2) se replace
esttab inc_so_hhinc inc_so_hhinc_dec_pp_sqrt10 inc_so_incdiff2 ///
	using "$OUT\Tables\LISS\hybrid_all_inc_subj_obj.tex", b(2) se replace booktabs alignment(D{.}{.}{3})
	
	
	
	
*logistic models for polint and vote_next
foreach var of varlist vote_next polint01 {	
	xthybrid `var' hhinc_dec_pp_sqrt10 female age18 unemployed nlf edu_med edu_high migrant, clusterid(pid) family(binomial) link(logit) meglmopts(iterate(100)) full
	eststo m5_`var'_logit
}
*

esttab  m5_vote_next_logit  ///
		m5_polint01_logit using "$OUT\Tables\LISS\hybrid_all_logit.rtf", b(3) se replace
esttab  m5_vote_next_logit  ///
		m5_polint01_logit  using "$OUT\Tables\LISS\hybrid_all_logit.tex", b(3) se replace label nonumbers booktabs alignment(D{.}{.}{3}) scalars("N N")	///
	varlabels ///
				(R__sex "Sex" ///
				R__migrant "Migrant" ///
				R__edu_med "Edu: medium" ///
				R__edu_high "Edu: high" ///
				W__edu_med "Edu: medium" ///
				W__edu_high "Edu: high" ///
				B__edu_med "Edu: medium" ///
				B__edu_high "Edu: high" ///
				W__age18 "W: Age" ///
				B__age18 "B: Age" ///
				W__unemployed "W: Unemployed" ///
				B__unemployed "B: Unemployed" ///
				W__nlf "W: Not in Labor Force" ///
				B__nlf "B: Not in Labor Force" ///
				W__hhinc "W: Income" ///
				B__hhinc "B: Income" ///
				W__hhincXage "W: Inc. * Age" ///
				B__hhincXage "B: Inc. * Age" ///
				W__hhinc_pp_sqrt10 "W: Income (dec.)" ///
				B__hhinc_pp_sqrt10 "B: Income (dec.)" ///
				W__hhinc_pp_sqrtXage "W: Inc. (dec.) * Age" ///
				B__hhinc_pp_sqrtXage "B: Inc. (dec.) * Age" ///
				W__hhinc_sat "W: Sat. Income" ///
				B__hhinc_sat "B: Sat. Income" ///
				W__hhinc_satXage "W: Sat. Inc. * Age" ///
				B__hhinc_satXage "B: Sat. Inc. * Age" ///
				_cons "Constant")	




*************************
* FE models
*************************

* restriction: variables need to be at least in 4 waves


* FE model
foreach var of varlist polint01 effint10  part10  vote_next {
	xtreg `var' L(0/3).hhinc F1.hhinc age18 unemployed nlf edu_med edu_high, fe
	eststo fe0_`var'_inc
	regsave  F1.hhinc hhinc L1.hhinc L2.hhinc L3.hhinc using "$OUT2\LISS\liss_coef_lag_hhinc.dta", ci level(95) autoid append
	xtreg `var' L(0/3).loginc F1.loginc age18 unemployed nlf edu_med edu_high, fe
	eststo fe_`var'_loginc
	regsave F1.loginc loginc L1.loginc L2.loginc L3.loginc using "$OUT2\LISS\liss_coef_lag_loginc.dta", ci level(95) autoid append
	xtreg `var' L(0/3).hhinc_dec_pp_sqrt10 F1.hhinc_dec_pp_sqrt10 age18 unemployed nlf edu_med edu_high, fe
	eststo fe_`var'_inc_pp_s
	regsave F1.hhinc_dec_pp_sqrt10 hhinc_dec_pp_sqrt10 L1.hhinc_dec_pp_sqrt10 L2.hhinc_dec_pp_sqrt10 L3.hhinc_dec_pp_sqrt10 using "$OUT2\LISS\liss_coef_lag_hhinc_dec_pp_sqrt.dta", ci level(95) autoid append
	xtreg `var' L(0/3).hhinc_sat10 F1.hhinc_sat10 age18 unemployed nlf edu_med edu_high, fe
	eststo fe_`var'_inc_sat
	regsave F1.hhinc_sat10 hhinc_sat10 L1.hhinc_sat10 L2.hhinc_sat10 L3.hhinc_sat10 using "$OUT2\LISS\liss_coef_lag_hhinc_sat.dta", ci level(95) autoid append
	xtreg `var' L(0/3).incdiff2 F1.incdiff2 age18 unemployed nlf edu_med edu_high, fe
	eststo fe_`var'_incdiff2
	regsave F1.incdiff2 incdiff2 L1.incdiff2 L2.incdiff2 L3.incdiff2 using "$OUT2\LISS\liss_coef_lag_incdiff2.dta", ci level(95) autoid append
}



esttab fe_vote_next_incdiff2 fe_polint01_incdiff2 fe_effint10_incdiff2 fe_part10_incdiff2   ///
	using "$OUT\Tables\LISS\fe_lag_all_hhinc_incdiff2.rtf", b(3) se replace		
esttab fe_vote_next_incdiff2 fe_polint01_incdiff2 fe_effint10_incdiff2 fe_part10_incdiff2  ///
	using "$OUT\Tables\LISS\fe_lag_all_hhinc_incdiff2.tex", b(3) se replace  label nonumbers booktabs scalars("N N")	///
	varlabels ///
				(edu_med "Edu: medium" ///
				edu_high "Edu: high" ///
				age18 "Age" ///
				unemployed "Unemployed" ///
				nlf "Not in Labor Force" ///
				hhinc "Income\textsubscript{t0}" ///
				L.hhinc "Income\textsubscript{t-1}" ///
				L2.hhinc "Income\textsubscript{t-2}" ///
				L3.hhinc "Income\textsubscript{t-3}" ///
				F.hhinc "Income\textsubscript{t+1}" ///
				hhinc_dec_pp_sqrt10 "Income (dec.)\textsubscript{t0}" ///
				L.hhinc_dec_pp_sqrt10 "Income (dec.)\textsubscript{t-1}" ///
				L2.hhinc_dec_pp_sqrt10 "Income (dec.)\textsubscript{t-2}" ///
				L3.hhinc_dec_pp_sqrt10 "Income (dec.)\textsubscript{t-3}" ///
				F.hhinc_dec_pp_sqrt10 "Income (dec.)\textsubscript{t+1}" ///
				hhinc_sat "Sat. Income\textsubscript{t0}" ///
				L.hhinc_sat "Sat. Income\textsubscript{t-1}" ///
				L2.hhinc_sat "Sat. Income\textsubscript{t-2}" ///
				L3.hhinc_sat "Sat. Income\textsubscript{t-3}" ///
				F.hhinc_sat "Sat. Income\textsubscript{t+1}" ///
				incdiff2 "Inc. Shock\textsubscript{t0}" ///
				L.incdiff2 "Inc. Shock\textsubscript{t-1}" ///
				L2.incdiff2 "Inc. Shock\textsubscript{t-2}" ///
				L3.incdiff2 "Inc. Shock\textsubscript{t-3}" ///
				F.incdiff2 "Inc. Shock\textsubscript{t+1}" ///
				_cons "Constant")
	
	
		
*Cross-check: objective income on subjective income 
foreach var of varlist hhinc  hhinc_dec_pp_sqrt10 incdiff2 {
	xtreg hhinc_sat10 L(0/3).`var' F1.`var' age18 unemployed nlf edu_med edu_high, fe
	eststo m_`var'
	*regsave F1.`var' `var' L1.`var' L2.`var' L3.`var' using "$OUT2\LISS\liss_coef_lag_inc_subj_obj_`var'.dta", ci level(95) autoid append
}
esttab m_hhinc m_hhinc_dec_pp_sqrt10 m_incdiff2   ///
	using "$OUT\Tables\LISS\fe_lag_all_inc_subj_obj.rtf", b(3) se replace		
esttab m_hhinc m_hhinc_dec_pp_sqrt10 m_incdiff2   ///
	using "$OUT\Tables\LISS\fe_lag_all_inc_subj_obj.tex", b(3) se replace label nonumbers booktabs alignment(D{.}{.}{3})			

	
*	
* Error correction model: lagged DV	

foreach var of varlist polint01 effint10 part10 {
	
	// exogenous
	xtdpdsys `var' hhinc_dec_pp_sqrt10 age18 unemployed nlf edu_med edu_high, twostep vce(robust) lags(2) maxldep(2) maxlags(2) artests(2)
	estat abond
	eststo ec_exo_`var'_inc_pp_s
	
	// predetermined	
	xtdpdsys `var' age18 unemployed nlf edu_med edu_high, twostep vce(robust) lags(2) maxldep(2) maxlags(2) artests(2) pre(hhinc_dec_pp_sqrt10)
	estat abond
	eststo ec_pre_`var'_inc_pp_s
	
	// endogeneous
	xtdpdsys `var' age18 unemployed nlf edu_med edu_high, twostep vce(robust) lags(2) maxldep(2) maxlags(2) artests(2) end(hhinc_dec_pp_sqrt10)
	estat abond
	eststo ec_end_`var'_inc_pp_s
}


*
esttab ec_exo_polint01_inc_pp_s ec_pre_polint01_inc_pp_s ec_end_polint01_inc_pp_s ///
	ec_exo_effint10_inc_pp_s ec_pre_effint10_inc_pp_s ec_end_effint10_inc_pp_s ///
	ec_exo_part10_inc_pp_s ec_pre_part10_inc_pp_s ec_end_part10_inc_pp_s ///
	using "$OUT\Tables\LISS\ec_all_hhinc_dec_pp_sqrt.rtf", b(3) scalar(arm1 arm2) se replace
esttab ec_exo_polint01_inc_pp_s ec_pre_polint01_inc_pp_s ec_end_polint01_inc_pp_s ///
	ec_exo_effint10_inc_pp_s ec_pre_effint10_inc_pp_s ec_end_effint10_inc_pp_s ///
	ec_exo_part10_inc_pp_s ec_pre_part10_inc_pp_s ec_end_part10_inc_pp_s ///
	using "$OUT\Tables\LISS\ec_all_hhinc_dec_pp_sqrt.tex", b(3) scalar("N N" "arm1 ABT(1)" "arm2 ABT(1)") se replace booktabs alignment(D{.}{.}{3}) ///
	varlabels ///
				(edu_med "Edu: medium" ///
				edu_high "Edu: high" ///
				age18 "Age" ///
				unemployed "Unemployed" ///
				nlf "Not in Labor Force" ///
				hhinc "Income" ///
				hhinc_dec_pp_sqrt10 "Income (dec.)" ///
				hhinc_sat "Sat. Income" ///
				_cons "Constant" ///
				L.effint10 "Int. Efficacy\textsubscript{t-1}" ///
				L2.effint10 "Int. Efficacy\textsubscript{t-2}" ///
				L.polint01 "Pol. Interest\textsubscript{t-1}" ///
				L2.polint01 "Pol. Interest\textsubscript{t-2}" ///
				L.part10 "Pol. part. index\textsubscript{t-1}" ///
				L2.part10 "Pol. part. index\textsubscript{t-2}")
	

	




















